package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.AkunMasterEntity;
import com.kifas.entity.TypeAkunEntity;
import com.kifas.model.xml.AkunMasterModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.TypeAkunModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/AkunMasters")
public class AkunMasterController {

	private static Logger log = Logger.getLogger(AkunMasterController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<AkunMasterModel> getAkunMasters() {
		return transformList(entityManager.getAkunMasters());
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public AkunMasterModel getAkunMaster(@PathParam("id") String kdAkun) {
		return transform(entityManager.getAkunMaster(kdAkun));
	}

	@GET
	@Path("Leaf")
	@Produces(MediaType.TEXT_XML)
	public List<AkunMasterModel> getAkunLeaf() {
		return transformList(entityManager.getAkunLeaf());
	}

	@GET
	@Path("Leaf/Debet")
	@Produces(MediaType.TEXT_XML)
	public List<AkunMasterModel> getAkunLeafDebet() {
		return transformList(entityManager.getAkunLeafDebet());
	}

	@GET
	@Path("Leaf/Kredit")
	@Produces(MediaType.TEXT_XML)
	public List<AkunMasterModel> getAkunLeafKredit() {
		return transformList(entityManager.getAkunLeafKredit());
	}

	@POST
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus insert(JAXBElement<AkunMasterModel> xml) {
		AkunMasterModel model = xml.getValue();
		TypeAkunEntity typeAkun = entityManager.getTypeAkun(model.getTypeAkun().getIdType());
		AkunMasterEntity akunMaster = new AkunMasterEntity(model.getKodeAkun(), model.getNama(), model.getIdParentAkun(), model.isActive(),
				model.getDeskripsi(), model.getDk(), model.getNamaPanjang(), typeAkun, model.getCreatedBy());
		try {
			entityManager.persist(akunMaster);
		} catch (Exception e) {
			log.error("can not persist Akun Master entity", e);
			return new PersistStatus("AkunMasterEntity", "failed", "can not persist akun master entity");
		}
		return new PersistStatus("AkunMasterEntity", "success");
	}

	private List<AkunMasterModel> transformList(List<AkunMasterEntity> akunMasters) {
		List<AkunMasterModel> akunMasterModels = new ArrayList<AkunMasterModel>();
		for (AkunMasterEntity akunMaster : akunMasters) {
			akunMasterModels.add(transform(akunMaster));
		}
		return akunMasterModels;
	}

	private AkunMasterModel transform(AkunMasterEntity akunMaster) {
		TypeAkunEntity typeAkun = akunMaster.getTypeAkun();
		TypeAkunModel typeAkunModel = new TypeAkunModel(typeAkun.getIdType(), typeAkun.getNama(), typeAkun.getDeskripsi(), typeAkun.getKode(), typeAkun.getCreatedBy());
		return new AkunMasterModel(akunMaster.getKodeAkun(), akunMaster.getNama(), akunMaster.getDk(), akunMaster.getIdParentAkun(), akunMaster.isActive(),
				akunMaster.getDeskripsi(), akunMaster.getNamaPanjang(), typeAkunModel, typeAkun.getCreatedBy());
	}
}
